package com.zzj.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageInfo;
import com.zzj.bean.TabMsg;
import com.zzj.service.TabMsgService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * InnoDB free: 8192 kB 前端控制器
 * </p>
 *
 * @author 花
 * @since 2022-03-04
 */
@RestController
@RequestMapping("/msg")
@Api("留言Controller")
@CrossOrigin
public class TabMsgController {
    @Autowired
    private TabMsgService msgService;

    @GetMapping("/getAllMsg")
    @ApiOperation("管理员专属,获取所有信息")
    public PageInfo<TabMsg> getAllMsg(
            @RequestParam(required = false,defaultValue = "1") int pageNum,
            @RequestParam(required = false,defaultValue = "5") int pageSize){
        return msgService.getAllMsg(pageNum,pageSize);
    }

    @GetMapping("/getMsg")
    @ApiOperation("获取留言信息")
    public List<TabMsg> getMsg(){
        QueryWrapper<TabMsg> wrapper = new QueryWrapper<>();
        wrapper.eq("mstatus", "1");
        return msgService.list(wrapper);
    }


    @PostMapping("/addMsg")
    @ApiOperation("添加留言信息")
    public boolean addMsg(@RequestBody TabMsg msg){
        System.out.println("msg = " + msg);
        msg.setMstatus("1");
        msg.setMtime(LocalDateTime.now());
        return msgService.save(msg);
    }

    @DeleteMapping("/delMsg")
    @ApiOperation("删除留言信息")
    public boolean delMsg(String mid){
        TabMsg msg = msgService.getById(mid);
        msg.setMstatus("0");
        return msgService.updateById(msg);
    }
}

